package com.qdsg.ysg.doctor.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.MouldResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;


import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;

import org.jetbrains.annotations.NotNull;

/**
 * 处方模板列表
 * Created by HP on 2020/2/26.
 */

public class MouldActivity extends BaseActivity {

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.edt_search)
    EditText edt_search;

    MyAdapter departmentAdapter;
    int type;

    List<MouldResponse.Mould> departmentList = new ArrayList<>();


    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager(MouldActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        departmentAdapter = new MyAdapter();
        recyclerView.setAdapter(departmentAdapter);

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> getMouldList());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
        });
        edt_search.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {   // 按下完成按钮，这里和上面imeOptions对应
                getMouldList();
            }
            return false;
        });
    }

    @Override
    protected void initData() {
        type = getIntent().getIntExtra("type", 0);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_mould;
    }

    @Override
    protected void onResume() {
        getMouldList();
        super.onResume();
    }

    @OnClick(R.id.tv_add)
    void tv_add() {
        startActivity(AddMouldActivity.class);
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @NotNull
        @Override
        public MyAdapter.Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            MyAdapter.Holder myHolder;
            myHolder = new MyAdapter.Holder(LayoutInflater.from(MouldActivity.this).inflate(R.layout.item_mould, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(MyAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                if (type == 0) { // 0 详情  1 选择
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("obj", departmentList.get(position));
                    startActivity(AddMouldActivity.class, bundle);
                } else {
                    Intent mIntent = new Intent();
                    mIntent.putExtra("obj", departmentList.get(position));
                    setResult(RESULT_OK, mIntent);
                    finish();
                }
            });
            holder.tv_department.setText(departmentList.get(position).name);
            holder.tv_info.setText(departmentList.get(position).diagnoseResult);
            holder.tv_time.setText(departmentList.get(position).createTime);
        }

        @Override
        public int getItemCount() {
            return departmentList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_department, tv_info, tv_time;

            public Holder(View itemView) {
                super(itemView);
                tv_department = itemView.findViewById(R.id.tv_department);
                tv_info = itemView.findViewById(R.id.tv_info);
                tv_time = itemView.findViewById(R.id.tv_time);
            }
        }
    }


    private void getMouldList() {
        RestProxy.getInstance().getMouldList(edt_search.getText().toString(), new Observer<MouldResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MouldResponse mouldResponse) {
                departmentList.clear();
                departmentList.addAll(mouldResponse.data.records);
                departmentAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
                ToastUtil.onError(MouldActivity.this, e);

            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }
}
